package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.CourseType;
import com.think.teaching.service.impl.CourseTypeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程类型 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags =  "课程类型接口文档")
public class CourseTypeController extends BaseController{

	private final CourseTypeServiceImpl courseTypeService;

	public CourseTypeController(CourseTypeServiceImpl courseTypeService) {
		this.courseTypeService = courseTypeService;
	}

	@ApiOperation("添加新的课程类型")
	@PostMapping("admin/courseType")
	public Result AddCourseType(@RequestBody()CourseType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			
			CourseType courseType = courseTypeService.GetCourseTypeByName(data.getTypeName());
			if (courseType != null ) {
				result.setCode(ResultCode.COURSE_TYPE_ALREADY_EXIST.code());
				result.setMsg(String.format("名为'%s'的类型已经存在", data.getTypeName()));//(ResultCode.COURSE_TYPE_ALREADY_EXIST);
				return result;
			}
			courseType = courseTypeService.AddCourseType(data);
			if( courseType!= null) {
				return Result.success(courseType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新课程类型")
	@RequestMapping(value = "admin/courseType",method = RequestMethod.PUT)
	public Result UpdateCourseType(@RequestBody()CourseType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			CourseType courseType = courseTypeService.GetCourseTypeByName(data.getTypeName());
			if (courseType != null && !courseType.getId().equals(data.getId())) {
				result.setCode(ResultCode.COURSE_TYPE_ALREADY_EXIST.code());
				result.setMsg(String.format("名为'%s'的类型已经存在", data.getTypeName()));//(ResultCode.COURSE_TYPE_ALREADY_EXIST);
				return result;
			}
			courseType = courseTypeService.UpdateCourseType(data);
			if( courseType!= null) {
				return Result.success(courseType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除课程类型")
	@RequestMapping(value = "admin/courseType/{id}",method =
			RequestMethod.DELETE)
	public Result DeleteCourseType(@PathVariable("id")Integer id) {
		CourseType courseType = courseTypeService.getById(id);
		if( courseType== null) {
			return Result.failure(ResultCode.COURSE_TYPE_NOT_EXIST);
		}
		else {
			if (courseTypeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
		}
	}
	
	@ApiOperation("获取课程类型详情")
	@RequestMapping(value = "admin/courseType/{id}",method = RequestMethod.GET)
	public Result GetCourseType(@PathVariable("id")Integer id) {
		CourseType courseType = courseTypeService.getById(id);
		if( courseType== null) {
			return Result.failure(ResultCode.COURSE_TYPE_NOT_EXIST);
		}
		else {
			
			return Result.success(courseType);
		}
	}
	
	@ApiOperation("管理员修改课程类型状态")
	@RequestMapping(value = "admin/courseType/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateCourseTypeState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		CourseType courseType = courseTypeService.getById(id);
		if( courseType== null) {
			return Result.failure(ResultCode.COURSE_TYPE_NOT_EXIST);
		}
		else {
			try {
				courseType = courseTypeService.SetCourseTypeState(id, state);
				return Result.success(courseType);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索课程类型")
	@RequestMapping(value = "user/courseType/search",method = RequestMethod.GET)
	public Result UserSearchCourseType(@RequestParam("key")String key) {
		return Result.success(courseTypeService.SearchCourseTypeWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索课程类型")
	@RequestMapping(value = "admin/courseType/search",method = RequestMethod.GET)
	public Result AdminSearchCourseType(@RequestParam("key")String key) {
		return Result.success(courseTypeService.SearchCourseTypeWithKey(key, false));
	}
	
	@ApiOperation("管理员获取课程类型列表")
	@RequestMapping(value = "admin/courseTypes/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListCourseType(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(courseTypeService.ListCourseTypeWithTagAndType(count, page, 0, 0, false));
	}
	
	@ApiOperation("用户获取课程类型列表")
	@RequestMapping(value = "open/courseTypes/{page}/{count}",method = RequestMethod.GET)
	public Result UserListCourseType(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(courseTypeService.ListCourseTypeWithTagAndType(count, page, 0, 0, true));
	}
}

